package com.aipartner.controller;

import com.aipartner.common.Result;
import com.aipartner.dto.LoginResponse;
import com.aipartner.dto.UserInfoResponse;
import com.aipartner.dto.WechatLoginRequest;
import com.aipartner.service.UserService;
import com.aipartner.util.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * User Management Controller
 * 
 * @author AI Partner Team
 * @since 2024-01-20
 */
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
@Validated
@Slf4j
@Api(tags = "User Management")
public class UserController {
    
    private final UserService userService;
    
    /**
     * WeChat login
     */
    @PostMapping({"/wechat/login", "/login/wechat", "/wechat-login"})
    @ApiOperation("WeChat Mini Program Login")
    public Result<LoginResponse> wechatLogin(@Valid @RequestBody WechatLoginRequest request) {
        return userService.wechatLogin(request);
    }
    
    /**
     * Get user information
     */
    @GetMapping("/info")
    @ApiOperation("Get Current User Information")
    public Result<UserInfoResponse> getUserInfo() {
        Long userId = UserContext.getCurrentUserId();
        if (userId == null) {
            return Result.fail("User not logged in");
        }
        return userService.getUserInfo(userId);
    }
    
    /**
     * Update user information
     */
    @PutMapping("/info")
    @ApiOperation("Update User Information")
    public Result<String> updateUserInfo(@Valid @RequestBody UserInfoResponse request) {
        Long userId = UserContext.getCurrentUserId();
        if (userId == null) {
            return Result.fail("User not logged in");
        }
        return userService.updateUserInfo(userId, request);
    }
}